package com.atguigu.yygh.user.controller.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.user.utils.IpUtil;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author ybl
 * @version 1.9
 * @date 2021/5/20 11:33
 */
@Api(tags = "用户登录注册接口")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

  @Autowired
  private UserInfoService userInfoService;

  @ApiOperation(value = "手机号登录")
  @PostMapping("login")
  public Result login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
    loginVo.setIp(IpUtil.getIpAddr(request));
    Map<String,Object> map = userInfoService.login(loginVo);
    return Result.ok(map);
  }

  // 用户认证接口
  @ApiOperation(value = "用户认证接口")
  @PostMapping("auth/userAuth")
  public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
    Long userId = AuthContextHolder.getUserId(request);
    userInfoService.userAuth(userId,userAuthVo);
    return Result.ok();
  }

  // 获取用户信息
  @ApiOperation(value = "获取用户id信息接口")
  @GetMapping("auth/getUserInfo")
  public Result getUserInfo(HttpServletRequest request) {
    Long userId = AuthContextHolder.getUserId(request);
    UserInfo userInfo = userInfoService.getById(userId);
    return Result.ok(userInfo);
  }
}
